﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Clock
{
    public partial class ClockForm : Form
    {
        
        public ClockForm()
        {
            InitializeComponent();
        }
        private Graphics g;
        private Pen pn1;
        private Pen pn2;
        private Pen pn3;
        private Thread td;
        private void ClockForm_Load(object sender, EventArgs e)
        {
            //g = CreateGraphics();
            this.BackColor = Color.Black;
            td= new Thread(Start);
            td.Name = "MyClock";
            td.Start();
        }
        private void ClockForm_Paint(object sender, PaintEventArgs e)
        {
            RunClock(e);
        }

        public void RunClock(PaintEventArgs e)
        {
            g = e.Graphics;
            //g.ResetTransform();
            pn1 = new Pen(Color .Red,3);
            pn2 = new Pen(Color .Yellow,1);
            pn3 = new Pen(Color.Chartreuse, 2);
            for (int i = 0; i<=59; i++)
            {
                int j=i*6;
                g.TranslateTransform(300, 220);
                g.RotateTransform(j);
                if (j % 5 == 0 && j % 90 !=0)
                {
                    g.DrawLine(pn3, 190, 0, 200, 0);
                }
                else
                {
                    if (j % 90 == 0)
                        g.DrawLine(pn1, 180, 0, 200, 0);
                    else
                        g.DrawLine(pn2, 195, 0, 200, 0);
                }
                g.ResetTransform();
               
            }
           //得到当前的时间信息
           
            float  sec = DateTime.Now.Second;
            float hour = DateTime.Now.Hour;
            float min = DateTime.Now.Minute;
            hour = hour + min / 60f + sec/ 3600f;
            min= min+ sec/ 60f;
            //秒针
            g.TranslateTransform(300,220);
            g.RotateTransform(sec*6-90);
            g.DrawLine(pn3,0,0,180,0);
            g.DrawLine(pn3, 190, 0, 200, 0);
            g.ResetTransform();
            //分针
            g.TranslateTransform(300, 220);
            g.RotateTransform(min * 6-90);
            g.DrawLine(pn3, 0, 0, 135, 0);
            g.ResetTransform();
            //时针
            g.TranslateTransform(300, 220);
            g.RotateTransform(hour*30-90);
            g.DrawLine(pn3, 0, 0, 100, 0);
            g.ResetTransform();
            SolidBrush sh = new SolidBrush(Color.Red);
            DateTime dt=DateTime .Now ;
            string showTime=string.Format("{0:D2}{1}{2:D2}{3}{4:D2}",dt.Hour,":",dt.Minute,":",dt.Second);
            string showDate=dt.ToString("yy-MM-dd");
            g.DrawString(showDate,new Font("宋体",15),sh,new PointF (260,275));
            g.DrawString(showTime, new Font("宋体", 15), sh, new PointF(260, 305));

        }
        public void Start()
        {
            while (true)
            {
                Invalidate();
                Thread.Sleep(1000);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            td.Abort();
            this.Close(); 
        }

    }
}
